<template>
  <div class="warning-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>库存预警</span>
          <div>
            <el-button type="primary" @click="loadData">刷新</el-button>
            <el-button @click="exportData">导出</el-button>
          </div>
        </div>
      </template>

      <el-alert
          title="库存预警说明"
          description="以下药材库存量较低，请及时补充库存"
          type="warning"
          show-icon
          style="margin-bottom: 20px;"
      />

      <el-table
          :data="warningList"
          v-loading="loading"
          stripe
          style="width: 100%"
      >
        <el-table-column prop="herbName" label="药材名称" width="150" />
        <el-table-column prop="batchNumber" label="批次号" width="120" />
        <el-table-column prop="quantity" label="当前库存(g)" width="100" align="center">
          <template #default="{ row }">
            <el-tag :type="getQuantityTagType(row.quantity)">
              {{ row.quantity }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="unitPrice" label="单价" width="100" align="center">
          <template #default="{ row }">
            ￥{{ row.unitPrice }}
          </template>
        </el-table-column>
        <el-table-column prop="storageLocation" label="存储位置" width="120" />
        <el-table-column prop="supplierName" label="供应商" width="120" />
        <el-table-column prop="purchaseDate" label="采购日期" width="120">
          <template #default="{ row }">
            {{ formatDate(row.purchaseDate) }}
          </template>
        </el-table-column>
        <el-table-column prop="expirationDate" label="过期日期" width="120">
          <template #default="{ row }">
            {{ formatDate(row.expirationDate) }}
          </template>
        </el-table-column>
        <el-table-column prop="daysUntilExpiry" label="剩余天数" width="100" align="center">
          <template #default="{ row }">
            <el-tag :type="getExpiryTagType(row.daysUntilExpiry)">
              {{ row.daysUntilExpiry }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="qualityNotes" label="质量备注" show-overflow-tooltip />
        <el-table-column label="操作" width="120" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleEdit(row)">编辑</el-button>
            <el-button type="primary" link @click="handleAddInventory(row)">补货</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="empty-state" v-if="!loading && warningList.length === 0">
        <el-empty description="暂无库存预警数据" />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { herbInventoryApi } from '@/api/admin'

const router = useRouter()
const loading = ref(false)
const warningList = ref([])

// 加载预警数据
const loadData = async () => {
  loading.value = true
  try {
    const res = await herbInventoryApi.getWarningList()
    warningList.value = res.data || []
    ElMessage.success(`加载完成，共 ${warningList.value.length} 条预警记录`)
  } catch (error) {
    ElMessage.error('加载预警数据失败')
    console.error('加载预警数据失败:', error)
  } finally {
    loading.value = false
  }
}

const exportData = () => {
  // 实现导出功能
  ElMessage.info('导出功能开发中...')
}

const handleEdit = (row) => {
  router.push(`/admin/herb-inventory/edit/${row.inventoryId}`)
}

const handleAddInventory = (row) => {
  // 跳转到新增库存页面，并预填药材信息
  router.push({
    path: '/admin/herb-inventory/add',
    query: { herbId: row.herbId, herbName: row.herbName }
  })
}

const formatDate = (date) => {
  if (!date) return '-'
  return new Date(date).toLocaleDateString()
}

const getQuantityTagType = (quantity) => {
  if (quantity <= 100) return 'danger'
  if (quantity <= 500) return 'warning'
  if (quantity <= 1000) return 'info'
  return 'success'
}

const getExpiryTagType = (days) => {
  if (days < 0) return 'danger'
  if (days <= 30) return 'warning'
  return 'success'
}

// 页面挂载时加载数据
onMounted(() => {
  loadData()
})
</script>

<style scoped>
.warning-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.empty-state {
  text-align: center;
  padding: 40px 0;
}
</style>
